package com.personnalManagerSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.personnalManagerSystem.model.Leavee;
import com.personnalManagerSystem.model.User;
import com.personnalManagerSystem.service.ILeaveeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-04-03
 */
@Controller
@RequestMapping("/leavee")
public class LeaveeController extends  BaseController {
    //重写分页
    public  Model baseList(Model model,
                           String option,
                           String searchContent,
                           Integer pageNum,
                           Integer pageSize,
                           ILeaveeService iLeaveeService){
        //设置默认的页码
        if(pageNum ==null || pageNum==0){
            pageNum=1;
        }
        //设置一页默认的显示记录
        if(pageSize==null  || pageSize==0){
            pageSize=5;
        }
        //创建一个构造器
        QueryWrapper<Leavee> wrapper = new QueryWrapper<>();
        if(option!=null
                && searchContent!=null
                && !"".equals(option)
                && !"".equals(searchContent)){
            wrapper.like(option,searchContent);
        }

        //设置分页
        Page<List<Leavee>> pageInfo = PageHelper.startPage(pageNum, pageSize);
        //在登录的时候将用户记录存储在了session中
        User user =(User) SecurityUtils.getSubject().getSession().getAttribute("user");
        if("普通用户".equals(user.getDescription())){
            //如果其角色是普通用户,那么只可以查询关于自己的一系列消息,不能出现跨用户访问,否则会出现资源泄露
            wrapper.eq("user_id",user.getUserId());
        }
        //查询数据
        iLeaveeService.queryLeaveeItem(wrapper);
        //设置页面信息
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("option",option);
        model.addAttribute("searchContent",searchContent);
        return model;
    }

    @RequestMapping("/list")
    public String list(Model model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){
        baseList(model,option,searchContent,pageNum,pageSize,iLeaveeService);
        return "view/leave";
    }

    @RequestMapping("/add")
    public String add(@RequestParam(value = "userId",required = false)Integer userId,
                      @RequestParam(value = "createTime",required = false)String createTime,
                      @RequestParam(value = "startTime" ,required = false)String startTime,
                      @RequestParam(value = "endTime",required = false)String endTime,
                      @RequestParam(value = "type",required = false)String type
                     ){
        //如果userId不存在,那么就跳转到全查页面,因为不排除会Id错误
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        User oneUser = iUserService.getOne(wrapper);
        if(oneUser==null){
            return "redirect:/leavee/list";
        }
        //set注入
        Leavee leavee = new Leavee();
        leavee.setUserId(userId);
        leavee.setCreateTime(LocalDateTime.parse(createTime));
        leavee.setStartTime(LocalDateTime.parse(startTime));
        leavee.setEndTime(LocalDateTime.parse(endTime));
        leavee.setType(type);

        //请假申请默认状态设置为0,即请假申请之后,等待管理员审核
        leavee.setLeaveState("0");
        //状态设置默认为0的时候,请假状态就是审核中
        leavee.setResult("审核中");
        //存储数据
        iLeaveeService.save(leavee);
        return "redirect:/leavee/list";
    }
    //请假撤销
    @RequestMapping("/del")
    public String del(@RequestParam("leaveId")Integer leaveId){
        iLeaveeService.removeById(leaveId);
        return "redirect:/leavee/list";
    }
    //请假编辑
    @RequestMapping("/edit")
    public String edit(
                       @RequestParam(value = "leaveId",required = false)Integer leaveId,
                       @RequestParam(value = "userId",required = false)Integer userId,
                       @RequestParam(value = "createTime",required = false)String createTime,
                       @RequestParam(value = "startTime" ,required = false)String startTime,
                       @RequestParam(value = "endTime",required = false)String endTime,
                       @RequestParam(value = "type",required = false)String type){
        System.out.println("leaveId:"+leaveId);
        //set注入
        Leavee leavee = new Leavee();
        leavee.setLeaveId(leaveId);
        leavee.setUserId(userId);
        leavee.setCreateTime(LocalDateTime.parse(createTime));
        leavee.setStartTime(LocalDateTime.parse(startTime));
        leavee.setEndTime(LocalDateTime.parse(endTime));
        leavee.setType(type);
        //iLeaveeService.saveOrUpdate(leavee);\
        iLeaveeService.updateById(leavee);
        return "redirect:/leavee/list";
    }
    @RequestMapping("/agree")
    /**
     * @Description  请假审核,默认在请假申请之后的请假状态未0,请假结果为审核中,然后根据多选框的result进行判断,如果
     * result的值为审核通过,就设置请假状态为1,如果不通过就设置请假状态为0
     * @author liqing
     * @data 2022/4/4  19:39
     * @param leaveId
     * @param result
     * @return java.lang.String
     */
    public String agree(@RequestParam("leaveId1")Integer leaveId,
                        @RequestParam("result")String result,
                        @RequestParam(value = "startTime1" ,required = false)String startTime,
                        @RequestParam(value = "endTime1",required = false)String endTime,
                        @RequestParam(value = "type1",required = false)String type){
        System.out.println("result:"+result);
        Leavee leavee = new Leavee();
        leavee.setLeaveId(leaveId);
        leavee.setStartTime(LocalDateTime.parse(startTime));
        leavee.setEndTime(LocalDateTime.parse(endTime));
        leavee.setType(type);
        if("审核通过".equals(result)){
            leavee.setLeaveState("1");
        }else if("审核未通过".equals(result)){
            leavee.setLeaveState("-1");
        }
        leavee.setResult(result);
        iLeaveeService.updateById(leavee);
        return "redirect:/leavee/list";
    }



}

